package com.hean.activity;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class DIYAdapter extends Activity {
	private int[] imgItemRes = { R.drawable.img_item0, R.drawable.img_item1,
			R.drawable.img_item2, R.drawable.img_item3, R.drawable.img_item4,
			R.drawable.img_item5, R.drawable.img_item6, R.drawable.img_item7,
			R.drawable.img_item8 };
	private String[] imgItemDesc = {"", "", "我正在减肥", "", "", "", "", "我是大熊猫",
			"" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.diyadapter);

		GridView gv = (GridView) findViewById(R.id.diyadapter_gv);

		gv.setAdapter(new ImageItemAdapter(DIYAdapter.this, imgItemRes,
				imgItemDesc));

		gv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ImageView imageView = (ImageView) view
						.findViewById(R.id.diyadapter_image);

				// 01
				// Animation animation = new RotateAnimation(0, 360 * 3,
				// Animation.RELATIVE_TO_SELF, 0.5f,
				// Animation.RELATIVE_TO_SELF, 0.5f);
				// animation.setDuration(1000);

				// 02
				// Animation animation = new ScaleAnimation(1, 0.5f, 1, 0.5f,
				// Animation.RELATIVE_TO_SELF, 0.5f,
				// Animation.RELATIVE_TO_SELF, 0.5f);
				// // animation.setFillAfter(true);
				// animation.setDuration(1000);

				// 03
				// Animation animation = new AlphaAnimation(1f, 0.1f);
				// animation.setDuration(2000);

				// 04
				// AnimationSet animation = new AnimationSet(Activity07.this,
				// null);
				// animation.addAnimation(new AlphaAnimation(1f, 0.1f));
				// animation.addAnimation(new ScaleAnimation(1, 0.5f, 1, 0.5f,
				// Animation.RELATIVE_TO_SELF, 0.5f,
				// Animation.RELATIVE_TO_SELF, 0.5f));
				// animation.addAnimation(new RotateAnimation(0, 360 * 3,
				// Animation.RELATIVE_TO_SELF, 0.5f,
				// Animation.RELATIVE_TO_SELF, 0.5f));
				// animation.setDuration(2000);

				// 05
				// AnimationSet animation = new AnimationSet(Activity07.this,
				// null);
				// Animation a1 = new RotateAnimation(0, 360 * 3,
				// Animation.RELATIVE_TO_SELF, 0.5f,
				// Animation.RELATIVE_TO_SELF, 0.5f);
				// Animation a2 = new ScaleAnimation(1, 0.5f, 1, 0.5f,
				// Animation.RELATIVE_TO_SELF, 0.5f,
				// Animation.RELATIVE_TO_SELF, 0.5f);
				//
				// Animation a3 = new RotateAnimation(0, -360 * 2,
				// Animation.RELATIVE_TO_SELF, 0.5f,
				// Animation.RELATIVE_TO_SELF, 0.5f);
				//
				// a1.setDuration(3000);
				// a2.setDuration(1000);
				// a3.setDuration(1000);
				//
				// a3.setStartOffset(3000);
				//
				// animation.addAnimation(a1);
				// animation.addAnimation(a2);
				// animation.addAnimation(a3);

				//逐帧动画
				AnimationDrawable animDrawable = new AnimationDrawable();
				// animDrawable.addFrame(Drawable, duration)

				for (int i = 0; i < 9; i++) {
					int resId = getResources().getIdentifier("img_item" + i,
							"drawable", "com.hean.activity");//"com.hean.activity"activity所在包
					animDrawable.addFrame(getResources().getDrawable(resId),
							125);
				}
				animDrawable.setOneShot(false);
				imageView.setImageDrawable(animDrawable);
				animDrawable.start();

				// imageView.startAnimation(animation);
			}
		});

	}

	/**
	 * 
	 * 自定义Adpater
	 * 
	 * @author steven
	 * 
	 */
	private class ImageItemAdapter extends BaseAdapter {
		private int[] imgItemRes;
		private String[] imgItemDesc;
		private Context ctx;
		// 将layout中的xml解析为对应的View
		private LayoutInflater inflater;

		public ImageItemAdapter(Context ctx, int[] imgItemRes,
				String[] imgItemDesc) {
			this.ctx = ctx;
			this.imgItemRes = imgItemRes;
			this.imgItemDesc = imgItemDesc;
			// 初始化LayoutInflater对象
			inflater = (LayoutInflater) this.ctx
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		/**
		 *返回要填充的Item的个数
		 */
		@Override
		public int getCount() {
			return imgItemRes.length;
		}

		/**
		 * 返回第position个Item
		 */
		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		/**
		 * 
		 */
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				//返回新的View
				// 加载布局文件
				convertView = inflater.inflate(R.layout.diyadapter_gv, null);
				// 填写数据
				ImageView imageView = (ImageView) convertView
						.findViewById(R.id.diyadapter_image);
				TextView textView = (TextView) convertView
						.findViewById(R.id.diyadapter_desc);

				imageView.setImageResource(imgItemRes[position]);
				textView.setText(imgItemDesc[position]);
			}
			return convertView;
		}

	}
}
